//
//  BDShopCouponsInfoViewController.m
//  BadBoy
//
//  Created by wen on 2017/7/5.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDShopCouponsInfoViewController.h"
#import "CallExternalProcessBehavior.h"
#import "BDUserModel.h"
#import "BDPayViewController.h"

@interface BDShopCouponsInfoViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation BDShopCouponsInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.coupondModel.title;
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kBDTopHeight, kBDScreenWidth, kBDScreenHeight - kBDTopHeight)];
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, kBDScreenWidth, 0)];
    topView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:topView];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.size = CGSizeMake(101, 76);
    imageView.top = 24;
    imageView.left = 15;
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.coupondModel.cover] placeholderImage:[UIImage imageNamed:@"shop_moren.jpg"]];
    [topView addSubview:imageView];
    
    UILabel *contnenLabel = [[UILabel alloc] init];
    contnenLabel.text = self.coupondModel.content;
    contnenLabel.font = [UIFont systemFontOfSize:14];
    contnenLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    contnenLabel.numberOfLines = 0;
    contnenLabel.width = kBDScreenWidth - imageView.right - 15 - 15;
    contnenLabel.height = [contnenLabel.text heightWithFont:contnenLabel.font width:contnenLabel.width];
    contnenLabel.left = imageView.right + 15;
    contnenLabel.top = imageView.top;
    [topView addSubview:contnenLabel];
    
    UIView *dashLine = [[UIView alloc] initWithFrame:CGRectMake(15, 0, kBDScreenWidth - 30, 1)];
    dashLine.top = MAX(contnenLabel.bottom, imageView.bottom) + 20;
    [dashLine drawLineLength:5 lineSpacing:2 lineColor:[UIColor colorWithHexString:@"#767676"] verticality:NO];
    [topView addSubview:dashLine];
    
    UILabel *curentPriceLabel = [[UILabel alloc] init];
    curentPriceLabel.font = [UIFont systemFontOfSize:13];
    curentPriceLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    NSString *string = [NSString stringWithFormat:ASLocalizedString(@"折扣价：¥%@"), [NSDecimalNumber decimalNumberWithString:self.coupondModel.discount_price]];
    NSMutableAttributedString *attribuString = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange range = [string rangeOfString:[NSString stringWithFormat:@"¥%@", [NSDecimalNumber decimalNumberWithString:self.coupondModel.discount_price]]];
    [attribuString setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"ff4f00"]} range:range];
    curentPriceLabel.attributedText = attribuString;
    [curentPriceLabel sizeToFit];
    curentPriceLabel.left = 15;
    curentPriceLabel.top = dashLine.bottom + 15;
    [topView addSubview:curentPriceLabel];
    
    UILabel *oldPriceLabel = [[UILabel alloc] init];
    oldPriceLabel.font = [UIFont systemFontOfSize:15];
    oldPriceLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    oldPriceLabel.text = [NSString stringWithFormat:ASLocalizedString(@"原价：¥%@"), [NSDecimalNumber decimalNumberWithString:self.coupondModel.price]];
    [oldPriceLabel sizeToFit];
    oldPriceLabel.bottom = curentPriceLabel.bottom;
    oldPriceLabel.left = curentPriceLabel.right + 15;
    [topView addSubview:oldPriceLabel];
    
    UILabel *peopleNumberLabel = [[UILabel alloc] init];
    peopleNumberLabel.font = [UIFont systemFontOfSize:15];
    peopleNumberLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    peopleNumberLabel.text = [NSString stringWithFormat:ASLocalizedString(@"推荐人数：%@人"), @(self.coupondModel.rec_num)];
    [peopleNumberLabel sizeToFit];
    peopleNumberLabel.top = curentPriceLabel.bottom + 15;
    peopleNumberLabel.left = 15;
    [topView addSubview:peopleNumberLabel];
    
    UIButton *qianggouButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [qianggouButton setTitle:ASLocalizedString(@"抢购")forState:UIControlStateNormal];
    [qianggouButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    qianggouButton.titleLabel.font = [UIFont systemFontOfSize:13];
    qianggouButton.backgroundColor = [UIColor colorWithHexString:@"4d7aad"];
    qianggouButton.size = CGSizeMake(65, 30);
    qianggouButton.centerY = peopleNumberLabel.top - 7.5;
    qianggouButton.right = kBDScreenWidth - 15;
    qianggouButton.layer.masksToBounds = YES;
    qianggouButton.layer.cornerRadius = 3;
    [topView addSubview:qianggouButton];
    [qianggouButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
    [[qianggouButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [BDUserModel judgeCurrentUserWithNormalBlock:^{
            BDPayViewController *payVC = [[BDPayViewController alloc] init];
            payVC.product = self.coupondModel;
            payVC.shopTitle = self.shopTitle;
            payVC.payType = BDProductPayTypeCoupon;
            payVC.productId = [NSString stringWithFormat:@"%@", @(self.coupondModel.my_id)];
            payVC.price = [NSDecimalNumber decimalNumberWithString:self.coupondModel.discount_price];
            [self.navigationController pushViewController:payVC animated:YES];
        }];
    }];
    
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, peopleNumberLabel.bottom + 15, kBDScreenWidth, 0.5)];
    line.backgroundColor = [UIColor colorWithHexString:@"#e5e5e5"];
    [topView addSubview:line];
    
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.font = [UIFont systemFontOfSize:15];
    timeLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    timeLabel.text = [NSString stringWithFormat:ASLocalizedString(@"有效期：%@ 至 %@"), self.coupondModel.startTimeString, self.coupondModel.endTimeString];
    [timeLabel sizeToFit];
    timeLabel.centerY = line.bottom + 20;
    timeLabel.left = 15;
    [topView addSubview:timeLabel];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, line.bottom + 40, kBDScreenWidth, 0.5)];
    line2.backgroundColor = [UIColor colorWithHexString:@"#e5e5e5"];
    [topView addSubview:line2];
    
    
    
    topView.height = line.bottom + 40;
    
    UIView *midView = [[UIView alloc] initWithFrame:CGRectMake(0, topView.bottom + 10, kBDScreenWidth, 95)];
    midView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:midView];
    
    UIImageView *managerCoverImage = [[UIImageView alloc] init];
    managerCoverImage.size = CGSizeMake(65, 65);
    managerCoverImage.left = 15;
    managerCoverImage.centerY = midView.height / 2.0;
    [managerCoverImage sd_setImageWithURL:[NSURL URLWithString:self.manager.cover] placeholderImage:[UIImage imageNamed:@"avatar"]];
    [midView addSubview:managerCoverImage];
    
    UILabel *managerNameLabel = [[UILabel alloc] init];
    managerNameLabel.text = self.manager.name;
    managerNameLabel.font = [UIFont systemFontOfSize:16];
    managerNameLabel.textColor = [UIColor colorWithHexString:@"#111111"];
    [managerNameLabel sizeToFit];
    managerNameLabel.left = managerCoverImage.right + 15;
    managerNameLabel.bottom = managerCoverImage.centerY - 7;
    [midView addSubview:managerNameLabel];
    
    UILabel *tishiLabel = [[UILabel alloc] init];
    tishiLabel.text = ASLocalizedString(@"提前预定惊喜不断…");
    tishiLabel.font = [UIFont systemFontOfSize:14];
    tishiLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    [tishiLabel sizeToFit];
    tishiLabel.left = managerNameLabel.left;
    tishiLabel.top = managerNameLabel.bottom + 15;
    [midView addSubview:tishiLabel];
    
    UIButton *callButton = [UIButton buttonWithType:UIButtonTypeCustom];
    callButton.backgroundColor = [UIColor colorWithHexString:@"4d7aad"];
    callButton.layer.cornerRadius = 5;
    callButton.size = CGSizeMake(65, 30);
    callButton.right = kBDScreenWidth - 15;
    callButton.centerY = midView.height / 2.0;
    [callButton setTitle:ASLocalizedString(@"联系")forState:UIControlStateNormal];
    callButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [callButton setImage:[UIImage imageNamed:@"lianxi"] forState:UIControlStateNormal];
    [callButton setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 5)];
    [midView addSubview:callButton];
    [[callButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [CallExternalProcessBehavior phoneWithPhoneNumber:self.manager.mobile callFinished:^{
            NSDictionary *dict = @{MerchantType_callShop:self.shopTitle};
            [MobClick event:MerchantName_callShop attributes:dict];
        }];
    }];
    [callButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, midView.bottom + 15, kBDScreenWidth, 0)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:bottomView];
    
    UILabel *xuzhiLabel = [[UILabel alloc] init];
    xuzhiLabel.font = [UIFont systemFontOfSize:14];
    xuzhiLabel.textColor = [UIColor colorWithHexString:@"4d7aad"];
    xuzhiLabel.text = ASLocalizedString(@"购买须知");
    [xuzhiLabel sizeToFit];
    xuzhiLabel.left = 15;
    xuzhiLabel.centerY = 35 / 2.0;
    [bottomView addSubview:xuzhiLabel];
    
    UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 35, kBDScreenWidth, 0.5)];
    bottomLine.backgroundColor = [UIColor colorWithHexString:@"#e5e5e5"];
    [bottomView addSubview:bottomLine];
    
    UILabel *conLabel = [[UILabel alloc] init];
    conLabel.text = ASLocalizedString(@"1.电话咨询，询问经理是否有此优惠套餐。\n2.选择符合优惠套餐，线上支付。\n3.支付成功，在我的预定中可获得二维码。\n4.进店消费，商家扫码二维码，确认消费。");
    conLabel.font = [UIFont systemFontOfSize:14];
    conLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    conLabel.numberOfLines = 0;
    conLabel.width = kBDScreenWidth - 30;
    conLabel.height = [conLabel.text heightWithFont:conLabel.font width:conLabel.width];
    conLabel.top = bottomLine.bottom + 15;
    conLabel.left = 15;
    [bottomView addSubview:conLabel];
    
    bottomView.height = conLabel.bottom + 15;
    
    _scrollView.contentSize = CGSizeMake(kBDScreenWidth, MAX(bottomView.bottom, _scrollView.height));
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
